

#include "LPC23xx.h"
#include "io.h"
#include "sys_config.h"
#include "debug.h"
#include "Sys.h" 
#include "USART.h"
#include "Receivers.h"
#include "modem.h"
#include "macros.h"
#include "dev.h"
//#include "AuxPort.h"


/* static unsigned char UART0_TXD_Buffer[UART0_TXD_BUFFER_SIZE]; */
/* //static unsigned char UART1_TXD_Buffer[UART1_TXD_BUFFER_SIZE]; */
/* static unsigned char UART2_TXD_Buffer[UART2_TXD_BUFFER_SIZE]; */


#define   FDIV		((PCLK/16)/br)

void UART0_Init ( unsigned long br )
{

	PINSEL0 |= 0x00000050; // Select UART0 TXD/RXD

	U0IER = 0x00;                         // disable all interrupts
	U0IIR;                                // clear interrupt ID
	U0RBR;                                // clear receive register
	U0LSR;                                // clear line status register

	U0FCR = 7; // Enable and clear FIFO's ; trigger level(bit7,6) == 0 -> trigger on each byte 
	U0LCR = 0x83; // 8N1, enable Divisor latch bit
	
	/* U0DLL = (unsigned char)((PCLK/(br * 16))); // baud rate fixed to 19200 @ PCLK = 12 Mhz */
	/* U0DLM = (unsigned char)(((PCLK/(br * 16)))>>8 );  // baud; */


	//unsigned int Fdiv = ( PCLK / 16 ) / br;	/*baud rate */
	/* U0DLM = Fdiv / 256 ; */
	/* U0DLL = Fdiv % 256; */

	U0DLM = 0;
	U0DLL = 8;


	U0LCR = 3; // Disable Divisor latch bit

	// ISR
	VICIntSelect    &= ~(1UL<<6);
	VICVectAddr6     = (unsigned int)Aux_Port_Receiver; 
	VICIntEnClr      = (1UL<<6);
	VICVectPriority6 = 14;
	VICIntEnable     = (1UL<<6);

	U0IER = UIER_RBR;

}

void UART2_Init ( unsigned long br )
{
	PINSEL0 |= (unsigned long)(1UL<<20); // Select UART2 TXD
	PINSEL0 |= (unsigned long)(1UL<<22); // Select UART2 RXD

	PCONP |= (1UL<<24); 			  /* UART2 */
	
	U2IER = 0x00;                         // disable all interrupts
	U2IIR;                                // clear interrupt ID
	U2RBR;                                // clear receive register
	U2LSR;                                // clear line status register

	U2FCR = 7; // Enable and clear FIFO's
	U2LCR = 0x83; // 8N1, enable Divisor latch bit

	/* U2DLL = (unsigned char)((PCLK/(br * 16))); // baud rate fixed to 19200 @ PCLK = 12 Mhz */
	/* U2DLM = (unsigned char)(((PCLK/(br * 16)))>>8 );  // baud; */

	/* U2DLM = (unsigned char)FDIV/256; */
	/* U2DLL = (unsigned char)FDIV%256; */

	U2DLM = 0;
	U2DLL = 8;



	U2LCR = 3; // Disable Divisor latch bit

	VICIntSelect    	&= ~(1UL<<28);
	VICVectAddr28     = (unsigned int)Modem_Receiver;
	VICIntEnClr      	= (1UL<<28);
	VICVectPriority28 = 13;
	VICIntEnable     	= (1UL<<28);
	
	U2IER = UIER_RBR;
}


void UART3_Init ( unsigned long br )
{
	PINSEL1 |= (unsigned long)(3UL<<18); // Select UART3 TXD
	PINSEL1 |= (unsigned long)(3UL<<20); // Select UART3 RXD

	PCONP |= (1UL<<25); 			  /* UART3 */
	
	U3IER = 0x00;                         // disable all interrupts
	U3IIR;                                // clear interrupt ID
	U3RBR;                                // clear receive register
	U3LSR;                                // clear line status register

	U3FCR = 7; // Enable and clear FIFO's
	U3LCR = 0x83; // 8N1, enable Divisor latch bit

	/* U3DLL = (unsigned char)((PCLK/(br * 16))); // baud rate fixed to 19200 @ PCLK = 12 Mhz */
	/* U3DLM = (unsigned char)(((PCLK/(br * 16)))>>8 );  // baud; */

	U3DLM = (unsigned char)FDIV/256;
	U3DLL = (unsigned char)FDIV%256;


	U3LCR = 3; // Disable Divisor latch bit

/* 	VICIntSelect    	&= ~(1UL<<28); */
/* 	VICVectAddr28     = (unsigned int)Modem_RXD; */
/* 	VICIntEnClr      	= (1UL<<28); */
/* 	VICVectPriority28 = 13; */
/* 	VICIntEnable     	= (1UL<<28); */
	
/* 	U3IER = UIER_RBR; */
}


void Init_UARTs ( void )
{	
//	Init_ModemReceiver();
	UART0_Init(115200);
	UART2_Init(115200);
	//UART3_Init(9600);
	
	MODEM_RTS_DIR = PIN_OUTPUT;
	MODEM_RTS = 0;
	MODEM_CTS_DIR = PIN_INPUT;
	
}

/* ------------------------------------------------------- */

void putch ( unsigned char ch, int Dev )
{
//	unsigned long CTS_TimeOut = 80000000; /* ~16 Sec */

	if ( Dev == DEV_MODEM )
	{

#ifdef   FLOW_CONTROL		
		while ( MODEM_CTS != 0 )
		{
			if ( --CTS_TimeOut == 0 )
			{
				ModemStatus = MODEM_NOT_ANSWER;
				return;
			}
		}
#endif

		while (!(U2LSR & 0x20))
			continue;
		U2THR = ch; 
	}
	else
		if ( Dev == DEV_PORT_A )
		{
			while (!(U0LSR & 0x20))
				continue;
			U0THR = ch;
		}
		else
			if ( Dev == DEV_PRINTER )
			{
				while (!(U3LSR & 0x20))
					continue;
				U3THR = ch;
			}
}

void putsf( const char *s, int Port )
{
	while ( *s )	
		putch(*s++, Port);
}

void putstr( char *s, int Port )
{
	while ( *s )	
		putch(*s++, Port );
}

void putds (unsigned char *s, unsigned int length, unsigned char Port )		// put a string with define string length
{
	while ( length-- )
	{
		putch( *s++, Port );
	}
}



/* --------------------------------------------------------------------------------------------------- */


/* void UART1_Init ( unsigned long br ) */
/* { */
/* /\* 	PINSEL0 |= 0x40000000; // Select UART1 TXD *\/ */
/* /\* 	PINSEL1 |= 0x00000001; // Select UART1 RXD *\/ */
/* /\* 	FIO2DIR &= ~(1UL<<1); *\/ */
/* /\* 	FIO2DIR &= ~(1UL<<0); *\/ */

/* 	PINMODE4 |= 0x0000000A; // no pull-up resistors */
/* 	PINSEL4 = 0; */
/* 	//PINSEL4 |= 0x0000000A; // Select UART1 TXD */
/* 	PINSEL4 |= 0x00000008; // Select UART1 RXD */
/* 	PINSEL4 |= 0x00000002; // Select UART1 TXD */

/* 	PINSEL4 |= 0x00000008; // Select UART1 RXD */
/* 	PINSEL4 |= 0x00000002; // Select UART1 TXD */

/* 	PINSEL4 |= 0x00000008; // Select UART1 RXD */
/* 	PINSEL4 |= 0x00000002; // Select UART1 TXD */
	
/* 	//	PINSEL10 = 0; */
	
	
/* 	U1IER = 0;		                       // disable all interrupts */
/* 	U1IIR;                                // clear interrupt ID */
/* 	U1RBR;                                // clear receive register */
/* 	U1LSR;                                // clear line status register */

/* 	U1FCR = 7; // Enable and clear FIFO's */
/* 	U1LCR = 0x83; // 8N1, enable Divisor latch bit */
/* 	/\* 	U1DLL = (unsigned char)(UART_BAUD(9600)); // baud rate fixed to 19200 @ PCLK = 12 Mhz *\/ */
/* /\* 	U1DLM = (unsigned char)((UART_BAUD(9600))>>8); // baud; *\/ */

/* 	U1DLL = (unsigned char)((PCLK/(br * 16))); // baud rate fixed to 19200 @ PCLK = 12 Mhz */
/* 	U1DLM = (unsigned char)(((PCLK/(br * 16)))>>8 ); // baud; */

/* 	U1LCR = 3; // Disable Divisor latch bit */


/*    U1MCR = 0; */
/* /\* 	FIO2DIR |= (1UL<<7); *\/ */
/* /\* 	FIO2PIN &= ~(1UL<<7); *\/ */

/* 	VICIntSelect    &= ~(1UL<<7); */
/* 	VICIntEnClr      = (1UL<<7); */
/* 	VICVectAddr7     = (unsigned int)GPS_Receiver; */
/* 	VICVectPriority7 = 15; */
/* 	VICIntEnable     = (1UL<<7); */
	
/* 	U1IER = UIER_RBR; */
/* } */

/* --------------------------------------------------------------------------------------------------- */

/* #define   TX_BUFSIZE		128 */
/* unsigned char TxBuf[TX_BUFSIZE]; */
/* void UART0_Tx_Int( void ) // Called from the main loop */
/* { */
/* 	int i; */

/* 	i = 16; */

/* 	if (U0LSR & 0x40) // transmit FIFO empty ? */
/* 	{ */
/* 		while(i && pData) */
/* 		{ */
/* 			U0THR = TxBuf[pTxBuf]; */
/* 			pData--; */
/* 			i--; */
/* 			pTxBuf++; */
/* 		} */
/* 	} */
/* } */

/* void SendString(const char *text) */
/* { */
/* 	if (pData == 0) // previous message send ? */
/* 	{ */
/* 		pTxBuf = 0; */
/* 		while(*text) */
/* 		{ */
/* 			TxBuf[pData++] = *text++; */
/* 			if(pData == TX_BUFSIZE) */
/* 				pData = 0; */
/* 		} */
/* 	} */
/* } */

/* static void Drive_UART0_TXD( void ) // Called from the main loop */
/* { */
/* 	unsigned char buf[16]; */
/* 	int i, bi; */
	
/* 	if ((U0LSR & 0x20)) // transmit FIFO empty ? */
/* 	{ */
/* 		i = Fifo_nRead(&Fifo_UART[SER_PORT_0], buf, 16); */
/* 		bi = 0; */
/* 		while( i ) */
/* 		{ */
/* 			U0THR = buf[bi]; */
/* 			bi++; */
/* 			i--; */
/* 		} */
/* 	} */
/* } */

/* static void Drive_UART1_TXD( void ) // Called from the main loop */
/* { */
/* 	unsigned char buf[16]; */
/* 	int i, bi; */
	
/* 	if (U1LSR & 0x20) // transmit FIFO empty ? */
/* 	{ */
/* 		i = Fifo_nRead(&Fifo_UART[SER_PORT_1], buf, 16); */
/* 		bi = 0; */
/* 		while( i ) */
/* 		{ */
/* 			U1THR = buf[bi]; */
/* 			bi++; */
/* 			i--; */
/* 		} */
/* 	} */
/* } */

/* static void Drive_UART2_TXD( void ) // Called from the main loop */
/* { */
/* 	unsigned char buf[16]; */
/* 	int i, bi; */

/* 	if (U2LSR & 0x20) // transmit FIFO empty ? */
/* 	{ */
/* 		i = Fifo_nRead(&Fifo_UART[SER_PORT_2], buf, 16); */
/* 		bi = 0; */
/* 		while( i ) */
/* 		{ */
/* 			U2THR = buf[bi]; */
/* 			bi++; */
/* 			i--; */
/* 		} */
/* 	} */
/* } */


/* void Drive_UARTs ( void ) */
/* { */
/* 	/\* Transmit *\/ */
/* 	Drive_UART0_TXD(); */
/* 	//Drive_UART1_TXD(); */
/* 	Drive_UART2_TXD(); */
/* } */

/* void UART1_Tx_Int(void) // Called from the main loop */
/* { */
/* 	char i = 16; */
/* 	if (U1LSR & 0x20) // transmit FIFO empty ? */
/* 	{ */
/* 		while (i && pData) */
/* 		{ */
/* 			U1THR = TxBuf[pTxBuf++]; */
/* 			pData--; */
/* 			i--; */
/* 		} */
/* 	} */
/* } */

/* int main (void) */
/* { */
/* 	UART1_Init(); */
/* 	while (1) */
/* 	{ */
/* 		SendString("Hello magnificent world of the LPC2000\r\n"); */
/* 		UART1_Tx_Int(); */
/* 	} */
/* }  */


// #define UART_BAUD(baud) (unsigned int)((PCLK / ((baud) * 16.0)) + 0.5)

// ----------------------------------------------------------------------------------------

/* int UART_putData ( char* Data, unsigned int ByteCount, unsigned int PortNumber ) */
/* { */
/* 	int Result; */

/* 	Result = Fifo_Write(&Fifo_UART[PortNumber], Data, ByteCount);  */
	
/* 	return Result; */
/* } */

/* int UART_puts ( void* ds, unsigned int PortNumber ) */
/* { */
/* 	int Result; */

/* 	char* s = ds; */

/* 	if ( s == NULL  ) */
/* 	{ */
/* 		return (-1); */
/* 	} */

/* 	Result = StrLen(s); */
/* 	if ( Result == 0 || Result == (-1) ) */
/* 	{ */
/* 		return (-1); */
/* 	} */
/* /\* 	Result = Fifo_Write( &Fifo_UART[PortNumber], s, (StrLen((char*)s)+1) );  *\/ */
/* /\* 	Fifo_UART[PortNumber].FifoBuffer[Fifo_UART[PortNumber].pWrite-1] = '\n'; /\\* ???? *\\/ *\/ */

/* 	Result = Fifo_Write( &Fifo_UART[PortNumber], s, Result );  */

/* 	if ( Result != -1 ) */
/* 	{ */
/* 		Result = 0; */
/* 	} */
	
/* 	return Result; */
/* } */
